/*
 *  Copyright (C) 2010 glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.abstracts;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.balisunrise.annotation.util.ReflectUtilities;

/**
 *
 * @author Glauco
 */
public abstract class AbstractFieldComponent extends AbstractEntityField{

    protected int alignment;
    public static final String PROP_ALIGMENT = "alignment";

    /**
     * Get the value of alignment
     *
     * @return the value of alignment
     */
    public int getAlignment() {
        return alignment;
    }

    /**
     * Set the value of alignment
     *
     * @param alignment new value of alignment
     */
    public void setAlignment(int aligment) {
        int oldAlignment = this.alignment;
        this.alignment = aligment;
        changeSupport.firePropertyChange(PROP_ALIGMENT, oldAlignment, aligment);
    }

    public abstract Object getVisualRepresentation();

    public boolean setProperty(String property, Object value){
        try{

            Object o = getVisualRepresentation();

            ReflectUtilities util = new ReflectUtilities(o);

            Field propField = util.getField(property);

            if(!propField.getType().isInstance(value))
                return false;

            if(propField.isAccessible()){
                propField.set(o, value);
            }else{
                Method propMethod = util.getSetMethod(property);
                propMethod.invoke(o, value);
            }
            return true;
        }catch(Throwable t){
            return false;
        }
    }
}
